#!/usr/bin/env python

# ------------------------------------------------------------
# ::                        WIGA-SOFT                       ::
# ------------------------------------------------------------
# lists.py
# Module for creating menu
# Created by: Wiktor`
# Version: 1.0
# ------------------------------------------------------------

from pao.models import Project, Employee, Task, Department, Budget

def projectsList(user):
    """
    Funkcija: projectsList(user) 
    Input: Username (user) 
    Output: List 
        [(<Projekto kodas>, <Projekto pavadinimas>),
         (<Projekto kodas>, <Projekto pavadinimas>),
         ( ... )]
    """
    
    allUserTasks = Task.objects.filter(employee__id__username = user)
    listOfProjects = []
    for e in allUserTasks:
        if e.project.id not in listOfProjects:
            listOfProjects.append((e.project.id, e.project.name))
    listOfProjects.sort()
    return listOfProjects
    
def managedProjectsList(user):
    """
    Funkcija: managedProjectsList(user) 
    Input: Username (user) 
    Output: List 
        [(<Projekto kodas>, <Projekto pavadinimas>),
         (<Projekto kodas>, <Projekto pavadinimas>),
         ( ... )]
    """
    
    usersManagedProjects = Project.objects.filter(manager__id__username = user)
    result = []
    for e in usersManagedProjects:
        if e.id not in result: # tikrinimas gal ir nereikalingas, bet del viso pikto...
            result.append((e.id, e.name))
    result.sort()
    return result
    
def departmentProjectsList(department):
    """
    Funkcija: departmentProjectsList(department) 
    Input: DepartmentID (department) 
    Output: List 
        [(<Projekto kodas>, <Pojekto vadovas>), (<Projekto kodas>, <Projekto vadovas>), ...]
    """
    
    allDepartmentTasks = Task.objects.all()
    allDepartmentTasks = allDepartmentTasks.filter(employee__department = department)
    listOfProjects = []
    result = []
    for e in allDepartmentTasks:
        if e.project.id not in listOfProjects:
            listOfProjects.append(e.project.id)
            tmp = (e.project.id, e.project.manager.id.username)
            result.append(tmp)
    result.sort()
    return result
    
def departmentEmployeeList(department):
    """
    Funkcija: departmentEmployeeList(department) 
    Input: DepartmentID (department) 
    Output: List 
        [<Username>, <Username>, ...]
    """
    
    allDepartmentEmployees = Employee.objects.all()
    allDepartmentEmployees = allDepartmentEmployees.filter(department__exact = department)
    listOfEmployees = []
    for e in allDepartmentEmployees:
        listOfEmployees.append(e.id.username)
    listOfEmployees.sort()
    return listOfEmployees
    
def departmentsList():
    """
    Funkcija: departmentsList() 
    Output: List 
        [(<DepartmentID>, <Departamento vadovas>), (<DepartmentID>, <Departamento vadovas>), ...]
    """
    
    allDepartments = Department.objects.all()
    result = []
    for e in allDepartments:
        tmp = (e.id, e.manager.id.username)
        result.append(tmp)
    result.sort()
    return result
    
def employeeList():
    """
    Funkcija: employeeList() 
    Output: List 
        [<Username>, <Username>, ...]
    """

    allEmployees = Employee.objects.all()
    result = []
    for e in allEmployees:
        result.append(e.id.username)
    result.sort()
    return result
    
def enterpriseEmployeeList():
    """
    Funkcija: enterpriseEmployeeList() 
    Output: List 
        [{'department': <department>,
          'employees':[<Username>,...]
         },
         {..},
         ...]
    """
    
    allEmployees = Department.objects.all().values("id", "name")

    allEmployees = allEmployees.order_by("id")
    result = []
    for e in allEmployees:
        departmentId = e['id']
        departmentName = e['name']
        if departmentName == None:
            department = departmentId
        else:
            department = departmentName
        employees = Employee.objects.filter(department__id = departmentId).values("id__username")
        emp = []
        for employee in employees:
            emp += [employee['id__username']]        
        result += [{"department":department, "employees": emp }]
    return result
